import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; import "react-loading-skeleton/dist/skeleton.css"; import { ClockIcon, HeartIcon } from "@heroicons/react/20/solid"; import { TvIcon, ArrowTrendingUpIcon, RectangleStackIcon, } from "@heroicons/react/24/outline"; import Head from "next/head"; import Image from "next/image"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import Layout from "../../components/layout"; import Link from "next/link"; import Content from "../../components/hero/content"; import Modal from "../../components/modal"; import { signIn, useSession } from "next-auth/react"; import AniList from "../../components/media/aniList"; import ListEditor from "../../components/listEditor"; import { closestMatch } from "closest-match"; const query = ` query ($username: String, $status: MediaListStatus) { MediaListCollection(userName: $username, type: ANIME, status: $status, sort: SCORE_DESC) { user { id name about (asHtml: true) createdAt avatar { large } statistics { anime { count episodesWatched meanScore minutesWatched } } bannerImage mediaListOptions { animeList { sectionOrder } } } lists { status name entries { id mediaId status progress score media { id status title { english romaji } episodes coverImage { large } } } } } } `; const infoQuery = `query ($id: Int) { Media(id: $id) { id type title { romaji english native } coverImage { extraLarge large color } bannerImage description episodes nextAiringEpisode { episode airingAt } averageScore popularity status startDate { year } duration genres relations { edges { relationType node { id type status title { romaji english userPreferred } coverImage { extraLarge large color } } } } recommendations { nodes { mediaRecommendation { id title { romaji } coverImage { extraLarge large } } } } } }`; export default function Info({ info, color }) { const { data: session } = useSession(); const [data, setData] = useState(null); const [episode, setEpisode] = useState(null); const [loading, setLoading] = useState(false); const [progress, setProgress] = useState(0); const [statuses, setStatuses] = useState(null); const [stall, setStall] = useState(false); const [domainUrl, setDomainUrl] = useState(""); const [showAll, setShowAll] = useState(false); const [open, setOpen] = useState(false); const [time, setTime] = useState(0); const { id } = useRouter().query; const [epiStatus, setEpiStatus] = useState("ok"); const [error, setError] = useState(null); const rec = info?.recommendations?.nodes.map( (data) => data.mediaRecommendation ); useEffect(() => { const { protocol, host } = window.location; const url = `${protocol}//${host}`; setDomainUrl(url); const defaultState = { data: null, // info: null, episode: null, loading: true, statuses: null, progress: null, stall: false, EpiStatus: "ok", error: null, }; // Reset all state variables to their default values Object.keys(defaultState).forEach((key) => { document.body.style.overflow = "auto"; const value = defaultState[key]; if (Array.isArray(value)) { value.length ? eval( `set${ key.charAt(0).toUpperCase() + key.slice(1) }(${JSON.stringify(value)})` ) : eval(`set${key.charAt(0).toUpperCase() + key.slice(1)}([])`); } else { eval( `set${key.charAt(0).toUpperCase() + key.slice(1)}(${JSON.stringify( value )})` ); } }); async function fetchData() { if (id) { setLoading(false); try { const [res] = await Promise.all([ fetch( `https://api.moopa.my.id/meta/anilist/info/${id?.[0]}?provider=zoro` ), // fetch("https://graphql.anilist.co/", { // method: "POST", // headers: { // "Content-Type": "application/json", // }, // body: JSON.stringify({ // query: infoQuery, // variables: { // id: id?.[0], // }, // }), // }), ]); const data = await res.json(); // const infos = await info.json(); if (res.status === 500) { setEpisode(null); setEpiStatus("error"); setError(data.message); } else if (res.status === 404) { window.location.href("/404"); } // setInfo(infos.data.Media); // setLog(data); // const textColor = setTxtColor(infos.data.Media.coverImage?.color); if (!data || data?.episodes?.length === 0) { const res = await fetch( `https://api.moopa.my.id/anime/gogoanime/${info.title.romaji}` ); const datas = await res.json(); // if (datas) { // const release = datas.results.map((i) => i.releaseDate); // const match = closestMatch(info.startDate.year, release); // const filter = datas.results.find((i) => i.releaseDate === match); // // const found = filter.find((i) => i.title === info.title.romaji); // // setLog(found); // if (filter) { // const res = await fetch( // `https://api.moopa.my.id/anime/gogoanime/info/${filter.id}` // ); // const dataA = await res.json(); // setEpisode(dataA.episodes); // // setLog(dataA); // } // } if (res.status === 500) { setEpisode(null); setEpiStatus("error"); setError(datas.message); } else { setEpisode([]); } // setColor({ // backgroundColor: `${data?.color || "#ffff"}`, // color: textColor, // }); } else { setEpisode(data.episodes); } // setColor({ // backgroundColor: `${data?.color || "#ffff"}`, // color: textColor, // }); if (session?.user?.name) { const response = await fetch("https://graphql.anilist.co/", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ query: query, variables: { username: session?.user?.name, }, }), }); const dat = await response.json(); const prog = dat.data.MediaListCollection; const gat = prog.lists.map((item) => item.entries); const git = gat.map((item) => item.find((item) => item.mediaId === parseInt(id[0])) ); const gut = git?.find((item) => item?.mediaId === parseInt(id[0])); if (gut) { setProgress(gut?.progress); if (gut.status === "CURRENT") { setStatuses({ name: "Watching", value: "CURRENT" }); } else if (gut.status === "PLANNING") { setStatuses({ name: "Plan to watch", value: "PLANNING" }); } else if (gut.status === "COMPLETED") { setStatuses({ name: "Completed", value: "COMPLETED" }); } else if (gut.status === "DROPPED") { setStatuses({ name: "Dropped", value: "DROPPED" }); } else if (gut.status === "PAUSED") { setStatuses({ name: "Paused", value: "PAUSED" }); } else if (gut.status === "REPEATING") { setStatuses({ name: "Rewatching", value: "REPEATING" }); } } } if (data.nextAiringEpisode) { setTime( convertSecondsToTime(data.nextAiringEpisode.timeUntilAiring) ); } setData(data); setLoading(true); } catch (error) { console.log(error); setTimeout(() => { window.location.reload(); }, 1000); } } } fetchData(); }, [id, session?.user?.name, info]); function handleOpen() { setOpen(true); document.body.style.overflow = "hidden"; } function handleClose() { setOpen(false); document.body.style.overflow = "auto"; } return ( <>
Episode {epi.number}
{epi.title && ("{epi.title}"
)} {index !== episode?.length - 1 && ( )}No Episodes Available
) ) : ( //// Something went wrong, can't retrieve any episodes :/ //
Something went wrong while retrieving data :/